package com.dpj.test;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "c.IncrementAndDecrement")
public class IncrementAndDecrement {
     static  int count=0;
    static Object lock=new Object();//对象锁

    @SneakyThrows
    public static void main(String[] args) {
        Thread t1=new Thread(()->{
            for (int i = 0; i <5000 ; i++) {
                //对临界区加锁
                synchronized (lock){
                    count++;
                }


            }
        },"t1");

        Thread t2=new Thread(()->{
            for (int i = 0; i <5000 ; i++) {
                //对临界区加锁
                synchronized (lock){
                    count--;
                }

            }
        },"t2");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        log.debug("{}",count);

    }
}
